Load all required libraries.
library(tidyverse)
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v ggplot2 3.3.5 v purrr 0.3.4
## v tibble 3.1.3 v dplyr 1.0.7
## v tidyr 1.1.3 v stringr 1.4.0
## v readr 2.0.0 v forcats 0.5.1
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
library(plotly)
##
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
##
## last_plot
## The following object is masked from 'package:stats':
##
## filter
## The following object is masked from 'package:graphics':
##
## layout
library(broom)
Read in raw data from RDS.
raw_data <- readRDS("./n1_n2_cleaned_cases.rds")
Make a few small modifications to names and data for visualizations.
final_data <- raw_data %>% mutate(log_copy_per_L = log10(mean_copy_num_L)) %>%
rename(Facility = wrf) %>%
mutate(Facility = recode(Facility,
"NO" = "WRF A",
"MI" = "WRF B",
"CC" = "WRF C"))
Seperate the data by gene target to ease layering in the final plot
#make three data layers
only_positives <<- subset(final_data, (!is.na(final_data$Facility)))
only_n1 <- subset(only_positives, target == "N1")
only_n2 <- subset(only_positives, target == "N2")
only_background <<-final_data %>%
select(c(date, cases_cum_clarke, new_cases_clarke, X7_day_ave_clarke)) %>%
group_by(date) %>% summarise_if(is.numeric, mean)
#specify fun colors
background_color <- "#7570B3"
seven_day_ave_color <- "#E6AB02"
marker_colors <- c("N1" = '#1B9E77',"N2" ='#D95F02')
#remove facilty C for now
#only_n1 <- only_n1[!(only_n1$Facility == "WRF C"),]
#only_n2 <- only_n2[!(only_n2$Facility == "WRF C"),]
only_n1 <- only_n1[!(only_n1$Facility == "WRF A" & only_n1$date == "2020-11-02"), ]
only_n2 <- only_n2[!(only_n2$Facility == "WRF A" & only_n2$date == "2020-11-02"), ]
Build the main plot
#first layer is the background epidemic curve
p1 <- only_background %>%
plotly::plot_ly() %>%
plotly::add_trace(x = ~date, y = ~new_cases_clarke,
type = "bar",
hoverinfo = "text",
text = ~paste('</br> Date: ', date,
'</br> Daily Cases: ', new_cases_clarke),
alpha = 0.5,
name = "Daily Reported Cases",
color = background_color,
colors = background_color,
showlegend = FALSE) %>%
layout(yaxis = list(title = "Clarke County Daily Cases", showline=TRUE)) %>%
layout(legend = list(orientation = "h", x = 0.2, y = -0.3))
#renders the main plot layer two as seven day moving average
p1 <- p1 %>% plotly::add_trace(x = ~date, y = ~X7_day_ave_clarke,
type = "scatter",
mode = "lines",
hoverinfo = "text",
text = ~paste('</br> Date: ', date,
'</br> Seven-Day Moving Average: ', X7_day_ave_clarke),
name = "Seven Day Moving Average Athens",
line = list(color = seven_day_ave_color),
showlegend = FALSE)
#renders the main plot layer three as positive target hits
p2 <- plotly::plot_ly() %>%
plotly::add_trace(x = ~date, y = ~mean_copy_num_L,
type = "scatter",
mode = "markers",
hoverinfo = "text",
text = ~paste('</br> Date: ', date,
'</br> Facility: ', Facility,
'</br> Target: ', target,
'</br> Copies/L: ', round(mean_copy_num_L, digits = 2)),
data = only_n1,
symbol = ~Facility,
marker = list(color = '#1B9E77', size = 8, opacity = 0.65),
showlegend = FALSE) %>%
plotly::add_trace(x = ~date, y = ~mean_copy_num_L,
type = "scatter",
mode = "markers",
hoverinfo = "text",
text = ~paste('</br> Date: ', date,
'</br> Facility: ', Facility,
'</br> Target: ', target,
'</br> Copies/L: ', round(mean_copy_num_L, digits = 2)),
data = only_n2,
symbol = ~Facility,
marker = list(color = '#D95F02', size = 8, opacity = 0.65),
showlegend = FALSE) %>%
layout(yaxis = list(title = "SARS CoV-2 Copies/L",
showline = TRUE,
type = "log",
dtick = 1,
automargin = TRUE)) %>%
layout(legend = list(orientation = "h", x = 0.2, y = -0.3))
#adds the limit of detection dashed line
p2 <- p2 %>% plotly::add_segments(x = as.Date("2020-03-14"),
xend = ~max(date + 10),
y = 3571.429, yend = 3571.429,
opacity = 0.35,
line = list(color = "black", dash = "dash")) %>%
layout(annotations = list(x = as.Date("2020-03-28"), y = 3.8, xref = "x", yref = "y",
text = "Limit of Detection", showarrow = FALSE))
p1
p2
Combine the two main plot pieces as a subplot
#seperate n1 and n2 frames by site
#n1
wrf_a_only_n1 <- subset(only_n1, Facility == "WRF A")
wrf_b_only_n1 <- subset(only_n1, Facility == "WRF B")
wrf_c_only_n1 <- subset(only_n1, Facility == "WRF C")
#n2
wrf_a_only_n2 <- subset(only_n2, Facility == "WRF A")
wrf_b_only_n2 <- subset(only_n2, Facility == "WRF B")
wrf_c_only_n2 <- subset(only_n2, Facility == "WRF C")
#rejoin the old data frames then seperate in to averages for each plant.
wrfa_both <- full_join(wrf_a_only_n1, wrf_a_only_n2)%>%
select(c(date, mean_total_copies)) %>%
group_by(date) %>%
summarize_if(is.numeric, mean) %>%
ungroup() %>%
mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke", "X7_day_ave_clarke", "Facility", "collection_num", "target", "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "mean_total_copies", "sd_total_copies", "log_copy_per_L")
wrfb_both <- full_join(wrf_b_only_n1, wrf_b_only_n2)%>%
select(c(date, mean_total_copies)) %>%
group_by(date) %>%
summarize_if(is.numeric, mean) %>%
ungroup() %>%
mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke", "X7_day_ave_clarke", "Facility", "collection_num", "target", "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "mean_total_copies", "sd_total_copies", "log_copy_per_L")
wrfc_both <- full_join(wrf_c_only_n1, wrf_c_only_n2)%>%
select(c(date, mean_total_copies)) %>%
group_by(date) %>%
summarize_if(is.numeric, mean) %>%
ungroup() %>%
mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke", "X7_day_ave_clarke", "Facility", "collection_num", "target", "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "mean_total_copies", "sd_total_copies", "log_copy_per_L")
#get max date
maxdate <- max(wrfa_both$date)
mindate <- min(wrfa_both$date)
Build loess smoothing figures figures
This makes the individual plots
#**************************************WRF A PLOT**********************************************
#add trendlines
#extract data from geom_smooth
#both extract
# *********************************span 0.6***********************************
#*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_botha <- ggplot(wrfa_both, aes(x = date, y = log_total_copies_both)) +
stat_smooth(aes(outfit=fit_botha<<-..y..), method = "loess", color = '#1B9E77',
span = 0.3, n = 511)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_botha
## `geom_smooth()` using formula 'y ~ x'
fit_botha
## [1] 12.92198 12.92389 12.92582 12.92776 12.92969 12.93162 12.93353 12.93540
## [9] 12.93723 12.93902 12.94074 12.94239 12.94396 12.94544 12.94681 12.94808
## [17] 12.94923 12.95024 12.95112 12.95184 12.95240 12.95279 12.95304 12.95318
## [25] 12.95323 12.95318 12.95304 12.95281 12.95250 12.95212 12.95166 12.95114
## [33] 12.95056 12.94992 12.94923 12.94850 12.94772 12.94690 12.94605 12.94518
## [41] 12.94428 12.94336 12.94243 12.94149 12.94055 12.93961 12.93867 12.93775
## [49] 12.93684 12.93595 12.93499 12.93389 12.93265 12.93128 12.92978 12.92819
## [57] 12.92649 12.92470 12.92284 12.92091 12.91892 12.91688 12.91481 12.91271
## [65] 12.91060 12.90848 12.90636 12.90426 12.90218 12.90014 12.89814 12.89620
## [73] 12.89432 12.89252 12.89081 12.88919 12.88768 12.88628 12.88453 12.88198
## [81] 12.87869 12.87472 12.87012 12.86497 12.85931 12.85321 12.84673 12.83993
## [89] 12.83286 12.82559 12.81817 12.81067 12.80314 12.79564 12.78824 12.78099
## [97] 12.77395 12.76718 12.76075 12.75471 12.74911 12.74403 12.73951 12.73562
## [105] 12.73242 12.72997 12.72769 12.72499 12.72193 12.71857 12.71496 12.71115
## [113] 12.70720 12.70316 12.69909 12.69505 12.69108 12.68725 12.68361 12.68020
## [121] 12.67710 12.67434 12.67199 12.67011 12.66873 12.66793 12.66776 12.66826
## [129] 12.66950 12.67153 12.67440 12.67817 12.68290 12.68938 12.69817 12.70894
## [137] 12.72135 12.73508 12.74981 12.76519 12.78090 12.79662 12.81201 12.82674
## [145] 12.84048 12.85291 12.86369 12.87435 12.88650 12.90002 12.91474 12.93054
## [153] 12.94726 12.96476 12.98289 13.00151 13.02047 13.03964 13.05885 13.07798
## [161] 13.09687 13.11538 13.13336 13.15068 13.16718 13.18271 13.19714 13.21032
## [169] 13.22211 13.23236 13.24263 13.25446 13.26769 13.28213 13.29761 13.31394
## [177] 13.33095 13.34846 13.36628 13.38426 13.40219 13.41992 13.43725 13.45401
## [185] 13.47003 13.48512 13.49910 13.51181 13.52305 13.53265 13.54044 13.54623
## [193] 13.54985 13.55112 13.54986 13.54590 13.53936 13.53067 13.52005 13.50774
## [201] 13.49396 13.47895 13.46294 13.44615 13.42882 13.41117 13.39344 13.37587
## [209] 13.35867 13.34208 13.32391 13.30215 13.27729 13.24983 13.22028 13.18912
## [217] 13.15686 13.12398 13.09100 13.05840 13.02668 12.99634 12.96788 12.94179
## [225] 12.91405 12.88090 12.84322 12.80191 12.75787 12.71197 12.66511 12.61818
## [233] 12.57207 12.52767 12.48587 12.44757 12.41365 12.38501 12.35800 12.32871
## [241] 12.29764 12.26525 12.23204 12.19849 12.16508 12.13229 12.10062 12.07054
## [249] 12.04255 12.01711 11.99472 11.97586 11.96020 11.94691 11.93573 11.92637
## [257] 11.91857 11.91205 11.90654 11.90178 11.89748 11.89338 11.88921 11.88469
## [265] 11.87955 11.87352 11.86826 11.86545 11.86486 11.86627 11.86945 11.87418
## [273] 11.88022 11.88736 11.89537 11.90402 11.91308 11.92233 11.93155 11.94050
## [281] 11.94896 11.95671 11.96351 11.96915 11.97339 11.97601 11.97679 11.97549
## [289] 11.97190 11.96800 11.96574 11.96481 11.96489 11.96569 11.96689 11.96818
## [297] 11.96926 11.96981 11.96954 11.96812 11.96525 11.96063 11.95394 11.94460
## [305] 11.93250 11.91804 11.90163 11.88365 11.86451 11.84460 11.82433 11.80409
## [313] 11.78429 11.76532 11.74758 11.73147 11.71739 11.70181 11.68150 11.65726
## [321] 11.62991 11.60027 11.56915 11.53736 11.50573 11.47506 11.44617 11.41988
## [329] 11.39700 11.37835 11.36474 11.35249 11.33773 11.32104 11.30295 11.28402
## [337] 11.26481 11.24587 11.22775 11.21100 11.19617 11.18383 11.17453 11.16881
## [345] 11.16722 11.16898 11.17279 11.17848 11.18587 11.19478 11.20504 11.21647
## [353] 11.22891 11.24216 11.25607 11.27044 11.28511 11.29990 11.31464 11.33079
## [361] 11.34977 11.37132 11.39518 11.42108 11.44878 11.47801 11.50850 11.54001
## [369] 11.57226 11.60501 11.63798 11.67092 11.70357 11.73567 11.76695 11.79717
## [377] 11.82605 11.85334 11.88137 11.91244 11.94625 11.98249 12.02086 12.06106
## [385] 12.10278 12.14573 12.18959 12.23407 12.27885 12.32365 12.36815 12.41206
## [393] 12.45506 12.49686 12.53715 12.57563 12.61200 12.64594 12.67717 12.70538
## [401] 12.73026 12.75402 12.77884 12.80439 12.83032 12.85629 12.88197 12.90701
## [409] 12.93109 12.95385 12.97496 12.99407 13.01086 13.02498 13.03609 13.04431
## [417] 13.05012 13.05377 13.05551 13.05557 13.05419 13.05161 13.04807 13.04381
## [425] 13.03907 13.03410 13.02912 13.02439 13.02014 13.01416 13.00440 12.99140
## [433] 12.97568 12.95774 12.93813 12.91734 12.89592 12.87436 12.85321 12.83297
## [441] 12.81416 12.79731 12.78294 12.76813 12.74998 12.72907 12.70595 12.68118
## [449] 12.65534 12.62899 12.60269 12.57700 12.55248 12.52971 12.50924 12.49164
## [457] 12.47747 12.46482 12.45151 12.43769 12.42353 12.40918 12.39482 12.38059
## [465] 12.36667 12.35321 12.34038 12.32833 12.31722 12.30723 12.29850 12.29042
## [473] 12.28231 12.27422 12.26623 12.25841 12.25082 12.24353 12.23662 12.23014
## [481] 12.22417 12.21877 12.21402 12.20997 12.20671 12.20410 12.20196 12.20030
## [489] 12.19911 12.19839 12.19814 12.19835 12.19903 12.20017 12.20177 12.20382
## [497] 12.20633 12.20929 12.21270 12.21656 12.22086 12.22561 12.23080 12.23642
## [505] 12.24249 12.24898 12.25591 12.26327 12.27105 12.27926 12.28790
#assign fits to a vector
both_trenda <- fit_botha
#extract y min and max for each
limits_botha <- ggplot_build(extract_botha)$data
## `geom_smooth()` using formula 'y ~ x'
limits_botha <- as.data.frame(limits_botha)
both_ymina <- limits_botha$ymin
both_ymaxa <- limits_botha$ymax
#reassign dataframes (just to be safe)
work_botha <- wrfa_both
#fill in missing dates to smooth fits
work_botha <- work_botha %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_botha <- work_botha$date
#create a new smooth dataframe to layer
smooth_frame_botha <- data.frame(date_vec_botha, both_trenda, both_ymina, both_ymaxa)
#WRF A
#plot smooth frames
p_wrf_a <- plotly::plot_ly() %>%
plotly::add_lines(x = ~date_vec_botha, y = ~both_trenda,
data = smooth_frame_botha,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_botha,
'</br> Median Log Copies: ', round(both_trenda, digits = 2)),
line = list(color = '#1B9E77', size = 8, opacity = 0.65),
showlegend = FALSE) %>%
layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_botha, ymin = ~both_ymina, ymax = ~both_ymaxa,
showlegend = FALSE,
opacity = 0.25,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_botha, #leaving in case we want to change
'</br> Max Log Copies: ', round(both_ymaxa, digits = 2),
'</br> Min Log Copies: ', round(both_ymina, digits = 2)),
name = "",
fillcolor = '#1B9E77',
line = list(color = '#1B9E77')) %>%
layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies",
showline = TRUE,
automargin = TRUE)) %>%
layout(xaxis = list(title = "Date")) %>%
layout(title = "WRF A") %>%
plotly::add_markers(x = ~date, y = ~log_total_copies_both,
data = wrfa_both,
hoverinfo = "text",
showlegend = FALSE,
text = ~paste('</br> Date: ', date,
'</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
marker = list(color = '#1B9E77', size = 6, opacity = 0.65))
p_wrf_a
save(p_wrf_a, file = "./plotly_objs/p_wrf_a.rda")
#**************************************WRF B PLOT**********************************************
#add trendlines
#extract data from geom_smooth
#both extract
# *********************************span 0.6***********************************
#*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_bothb <- ggplot(wrfb_both, aes(x = date, y = log_total_copies_both)) +
stat_smooth(aes(outfit=fit_bothb<<-..y..), method = "loess", color = '#D95F02',
span = 0.3, n = 511)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_bothb
## `geom_smooth()` using formula 'y ~ x'
fit_bothb
## [1] 12.56012 12.55884 12.55762 12.55647 12.55536 12.55431 12.55331 12.55236
## [9] 12.55144 12.55056 12.54972 12.54891 12.54812 12.54736 12.54661 12.54589
## [17] 12.54517 12.54446 12.54376 12.54307 12.54237 12.54166 12.54095 12.54024
## [25] 12.53953 12.53882 12.53811 12.53742 12.53674 12.53606 12.53541 12.53477
## [33] 12.53415 12.53355 12.53298 12.53243 12.53191 12.53143 12.53098 12.53056
## [41] 12.53018 12.52985 12.52956 12.52931 12.52911 12.52897 12.52887 12.52883
## [49] 12.52885 12.52893 12.52899 12.52895 12.52883 12.52865 12.52840 12.52810
## [57] 12.52777 12.52741 12.52704 12.52667 12.52631 12.52597 12.52566 12.52540
## [65] 12.52519 12.52505 12.52500 12.52503 12.52517 12.52542 12.52579 12.52631
## [73] 12.52697 12.52780 12.52879 12.52997 12.53135 12.53293 12.53456 12.53608
## [81] 12.53750 12.53883 12.54009 12.54128 12.54243 12.54354 12.54463 12.54570
## [89] 12.54678 12.54787 12.54898 12.55014 12.55135 12.55262 12.55397 12.55540
## [97] 12.55694 12.55860 12.56038 12.56230 12.56437 12.56661 12.56903 12.57163
## [105] 12.57444 12.57747 12.57984 12.58079 12.58048 12.57906 12.57667 12.57348
## [113] 12.56964 12.56529 12.56061 12.55573 12.55081 12.54601 12.54148 12.53738
## [121] 12.53385 12.53105 12.52913 12.52825 12.52857 12.53023 12.53338 12.53819
## [129] 12.54511 12.55429 12.56541 12.57816 12.59224 12.60733 12.62312 12.63930
## [137] 12.65556 12.67159 12.68707 12.70170 12.71516 12.72715 12.74039 12.75745
## [145] 12.77770 12.80056 12.82540 12.85164 12.87866 12.90586 12.93264 12.95839
## [153] 12.98250 13.00438 13.02342 13.03901 13.05447 13.07314 13.09445 13.11781
## [161] 13.14264 13.16839 13.19445 13.22027 13.24526 13.26885 13.29046 13.30951
## [169] 13.32543 13.33764 13.34802 13.35875 13.36974 13.38090 13.39213 13.40335
## [177] 13.41445 13.42536 13.43596 13.44617 13.45589 13.46504 13.47351 13.48122
## [185] 13.48806 13.49396 13.49881 13.50251 13.50499 13.50614 13.50587 13.50408
## [193] 13.50069 13.49560 13.48871 13.47994 13.46863 13.45443 13.43772 13.41884
## [201] 13.39817 13.37605 13.35286 13.32895 13.30468 13.28042 13.25652 13.23334
## [209] 13.21125 13.19061 13.16837 13.14169 13.11126 13.07775 13.04185 13.00424
## [217] 12.96559 12.92658 12.88791 12.85023 12.81424 12.78062 12.75005 12.72320
## [225] 12.69559 12.66289 12.62599 12.58577 12.54314 12.49898 12.45418 12.40963
## [233] 12.36624 12.32488 12.28644 12.25183 12.22193 12.19764 12.17582 12.15297
## [241] 12.12939 12.10538 12.08125 12.05729 12.03381 12.01112 11.98952 11.96930
## [249] 11.95077 11.93425 11.92001 11.90838 11.90003 11.89514 11.89324 11.89389
## [257] 11.89662 11.90097 11.90651 11.91275 11.91926 11.92557 11.93122 11.93577
## [265] 11.93875 11.93971 11.94122 11.94595 11.95359 11.96384 11.97638 11.99091
## [273] 12.00713 12.02471 12.04336 12.06276 12.08260 12.10259 12.12240 12.14174
## [281] 12.16029 12.17774 12.19379 12.20813 12.22044 12.23043 12.23778 12.24219
## [289] 12.24334 12.24303 12.24313 12.24347 12.24386 12.24412 12.24407 12.24352
## [297] 12.24229 12.24021 12.23709 12.23274 12.22698 12.21964 12.21053 12.19773
## [305] 12.17993 12.15793 12.13248 12.10436 12.07434 12.04320 12.01170 11.98062
## [313] 11.95072 11.92278 11.89758 11.87588 11.85845 11.84124 11.82012 11.79579
## [321] 11.76891 11.74016 11.71023 11.67979 11.64953 11.62012 11.59224 11.56657
## [329] 11.54379 11.52458 11.50962 11.49597 11.48053 11.46371 11.44595 11.42766
## [337] 11.40928 11.39124 11.37394 11.35783 11.34332 11.33084 11.32082 11.31368
## [345] 11.30985 11.30800 11.30659 11.30571 11.30545 11.30590 11.30716 11.30932
## [353] 11.31246 11.31669 11.32209 11.32876 11.33678 11.34626 11.35727 11.37101
## [361] 11.38837 11.40900 11.43255 11.45868 11.48705 11.51729 11.54908 11.58206
## [369] 11.61589 11.65022 11.68470 11.71899 11.75274 11.78561 11.81725 11.84731
## [377] 11.87544 11.90131 11.92712 11.95518 11.98526 12.01713 12.05055 12.08530
## [385] 12.12115 12.15786 12.19522 12.23298 12.27092 12.30880 12.34641 12.38350
## [393] 12.41986 12.45524 12.48942 12.52217 12.55326 12.58246 12.60954 12.63427
## [401] 12.65641 12.67805 12.70116 12.72540 12.75040 12.77584 12.80134 12.82656
## [409] 12.85116 12.87477 12.89706 12.91766 12.93622 12.95241 12.96586 12.97676
## [417] 12.98564 12.99271 12.99813 13.00210 13.00480 13.00642 13.00715 13.00717
## [425] 13.00666 13.00582 13.00482 13.00386 13.00312 13.00160 12.99829 12.99336
## [433] 12.98701 12.97939 12.97070 12.96111 12.95079 12.93993 12.92871 12.91729
## [441] 12.90586 12.89459 12.88367 12.87138 12.85616 12.83845 12.81869 12.79731
## [449] 12.77475 12.75145 12.72783 12.70433 12.68139 12.65945 12.63893 12.62028
## [457] 12.60393 12.58777 12.56965 12.54993 12.52895 12.50707 12.48464 12.46202
## [465] 12.43956 12.41762 12.39654 12.37668 12.35840 12.34205 12.32798 12.31501
## [473] 12.30178 12.28838 12.27491 12.26144 12.24807 12.23489 12.22199 12.20945
## [481] 12.19737 12.18584 12.17495 12.16478 12.15543 12.14673 12.13849 12.13069
## [489] 12.12333 12.11641 12.10992 12.10385 12.09821 12.09299 12.08818 12.08379
## [497] 12.07980 12.07622 12.07304 12.07025 12.06786 12.06585 12.06423 12.06299
## [505] 12.06212 12.06163 12.06150 12.06174 12.06233 12.06329 12.06459
#assign fits to a vector
both_trendb <- fit_bothb
#extract y min and max for each
limits_bothb <- ggplot_build(extract_bothb)$data
## `geom_smooth()` using formula 'y ~ x'
limits_bothb <- as.data.frame(limits_bothb)
both_yminb <- limits_bothb$ymin
both_ymaxb <- limits_bothb$ymax
#reassign dataframes (just to be safe)
work_bothb <- wrfb_both
#fill in missing dates to smooth fits
work_bothb <- work_bothb %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_bothb <- work_bothb$date
#create a new smooth dataframe to layer
smooth_frame_bothb <- data.frame(date_vec_bothb, both_trendb, both_yminb, both_ymaxb)
#WRF B
#plot smooth frames
p_wrf_b <- plotly::plot_ly() %>%
plotly::add_lines(x = ~date_vec_bothb, y = ~both_trendb,
data = smooth_frame_bothb,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_bothb,
'</br> Median Log Copies: ', round(both_trendb, digits = 2)),
line = list(color = '#D95F02', size = 8, opacity = 0.65),
showlegend = FALSE) %>%
layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_bothb, ymin = ~both_yminb, ymax = ~both_ymaxb,
showlegend = FALSE,
opacity = 0.25,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_bothb, #leaving in case we want to change
'</br> Max Log Copies: ', round(both_ymaxb, digits = 2),
'</br> Min Log Copies: ', round(both_yminb, digits = 2)),
name = "",
fillcolor = '#D95F02',
line = list(color = '#D95F02')) %>%
layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies",
showline = TRUE,
automargin = TRUE)) %>%
layout(xaxis = list(title = "Date")) %>%
layout(title = "WRF B") %>%
plotly::add_markers(x = ~date, y = ~log_total_copies_both,
data = wrfb_both,
hoverinfo = "text",
showlegend = FALSE,
text = ~paste('</br> Date: ', date,
'</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
marker = list(color = '#D95F02', size = 6, opacity = 0.65))
p_wrf_b
save(p_wrf_b, file = "./plotly_objs/p_wrf_b.rda")
#**************************************WRF C PLOT********************************************** #add trendlines #extract data from geom_smooth # *********************************span 0.6*********************************** #*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_bothc <- ggplot(wrfc_both, aes(x = date, y = log_total_copies_both)) +
stat_smooth(aes(outfit=fit_bothc<<-..y..), method = "loess", color = '#E7298A',
span = 0.3, n = 511)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_bothc
## `geom_smooth()` using formula 'y ~ x'
fit_bothc
## [1] 11.83755 11.84201 11.84652 11.85105 11.85561 11.86016 11.86470 11.86921
## [9] 11.87368 11.87809 11.88242 11.88666 11.89080 11.89481 11.89868 11.90241
## [17] 11.90596 11.90933 11.91251 11.91546 11.91819 11.92068 11.92290 11.92485
## [25] 11.92651 11.92786 11.92889 11.92958 11.92992 11.92995 11.92974 11.92929
## [33] 11.92862 11.92774 11.92667 11.92542 11.92401 11.92245 11.92075 11.91893
## [41] 11.91701 11.91498 11.91288 11.91071 11.90849 11.90622 11.90394 11.90164
## [49] 11.89934 11.89707 11.89482 11.89261 11.89047 11.88840 11.88641 11.88452
## [57] 11.88275 11.88067 11.87788 11.87444 11.87040 11.86581 11.86073 11.85521
## [65] 11.84931 11.84308 11.83658 11.82985 11.82296 11.81595 11.80888 11.80181
## [73] 11.79478 11.78786 11.78109 11.77454 11.76825 11.76227 11.75667 11.75150
## [81] 11.74680 11.74264 11.73907 11.73614 11.73391 11.73121 11.72695 11.72126
## [89] 11.71427 11.70614 11.69698 11.68693 11.67614 11.66474 11.65286 11.64064
## [97] 11.62822 11.61573 11.60332 11.59110 11.57923 11.56784 11.55706 11.54704
## [105] 11.53790 11.52978 11.52282 11.51716 11.51293 11.51027 11.50931 11.51019
## [113] 11.51305 11.51783 11.52428 11.53229 11.54171 11.55243 11.56432 11.57725
## [121] 11.59109 11.60571 11.62099 11.63679 11.65299 11.66947 11.68609 11.70272
## [129] 11.71924 11.73552 11.75143 11.76684 11.78163 11.79853 11.81994 11.84516
## [137] 11.87354 11.90438 11.93703 11.97080 12.00502 12.03902 12.07213 12.10366
## [145] 12.13294 12.15931 12.18209 12.20390 12.22770 12.25328 12.28043 12.30893
## [153] 12.33858 12.36916 12.40047 12.43229 12.46443 12.49665 12.52877 12.56056
## [161] 12.59181 12.62233 12.65188 12.68028 12.70730 12.73273 12.75637 12.77801
## [169] 12.79743 12.81443 12.83035 12.84663 12.86317 12.87989 12.89667 12.91343
## [177] 12.93008 12.94651 12.96265 12.97838 12.99362 13.00827 13.02224 13.03543
## [185] 13.04776 13.05911 13.06941 13.07855 13.08644 13.09299 13.09811 13.10169
## [193] 13.10364 13.10387 13.10229 13.09880 13.09372 13.08745 13.08002 13.07146
## [201] 13.06180 13.05107 13.03929 13.02650 13.01271 12.99796 12.98229 12.96571
## [209] 12.94825 12.92994 12.90820 12.88096 12.84904 12.81328 12.77449 12.73349
## [217] 12.69113 12.64821 12.60556 12.56401 12.52438 12.48749 12.45418 12.42526
## [225] 12.39543 12.35954 12.31863 12.27375 12.22594 12.17625 12.12573 12.07541
## [233] 12.02635 11.97960 11.93618 11.89716 11.86358 11.83648 11.81152 11.78401
## [241] 11.75449 11.72349 11.69155 11.65920 11.62698 11.59541 11.56504 11.53640
## [249] 11.51002 11.48643 11.46618 11.44979 11.43728 11.42802 11.42157 11.41753
## [257] 11.41547 11.41496 11.41558 11.41692 11.41853 11.42001 11.42093 11.42087
## [265] 11.41940 11.41610 11.41398 11.41606 11.42196 11.43126 11.44358 11.45852
## [273] 11.47568 11.49466 11.51507 11.53652 11.55859 11.58090 11.60306 11.62465
## [281] 11.64529 11.66458 11.68212 11.69752 11.71038 11.72029 11.72688 11.72972
## [289] 11.72845 11.72383 11.71710 11.70850 11.69829 11.68669 11.67397 11.66036
## [297] 11.64611 11.63147 11.61669 11.60200 11.58766 11.57391 11.56100 11.54465
## [305] 11.52120 11.49183 11.45770 11.42000 11.37988 11.33854 11.29714 11.25685
## [313] 11.21885 11.18430 11.15439 11.13029 11.11316 11.09872 11.08218 11.06399
## [321] 11.04459 11.02444 11.00398 10.98367 10.96395 10.94527 10.92807 10.91281
## [329] 10.89993 10.88988 10.88312 10.87887 10.87600 10.87442 10.87405 10.87478
## [337] 10.87652 10.87919 10.88269 10.88693 10.89181 10.89724 10.90314 10.90940
## [345] 10.91594 10.92392 10.93437 10.94700 10.96152 10.97765 10.99507 11.01352
## [353] 11.03269 11.05230 11.07205 11.09166 11.11083 11.12927 11.14670 11.16471
## [361] 11.18494 11.20718 11.23120 11.25678 11.28368 11.31169 11.34059 11.37014
## [369] 11.40012 11.43030 11.46047 11.49040 11.51987 11.54864 11.57650 11.60321
## [377] 11.62856 11.65233 11.67581 11.70040 11.72597 11.75240 11.77957 11.80735
## [385] 11.83563 11.86428 11.89317 11.92220 11.95124 11.98016 12.00884 12.03716
## [393] 12.06500 12.09224 12.11875 12.14442 12.16912 12.19273 12.21512 12.23619
## [401] 12.25579 12.27581 12.29787 12.32155 12.34641 12.37201 12.39792 12.42369
## [409] 12.44889 12.47308 12.49582 12.51668 12.53522 12.55100 12.56358 12.57364
## [417] 12.58220 12.58940 12.59533 12.60012 12.60388 12.60672 12.60877 12.61012
## [425] 12.61091 12.61124 12.61124 12.61100 12.61065 12.60882 12.60430 12.59743
## [433] 12.58859 12.57812 12.56640 12.55376 12.54058 12.52721 12.51400 12.50132
## [441] 12.48953 12.47898 12.47003 12.46085 12.44961 12.43661 12.42219 12.40667
## [449] 12.39038 12.37363 12.35676 12.34008 12.32392 12.30860 12.29445 12.28179
## [457] 12.27095 12.26090 12.25044 12.23963 12.22854 12.21723 12.20578 12.19423
## [465] 12.18266 12.17114 12.15972 12.14847 12.13746 12.12675 12.11641 12.10614
## [473] 12.09564 12.08496 12.07412 12.06318 12.05218 12.04115 12.03013 12.01917
## [481] 12.00831 11.99759 11.98704 11.97672 11.96665 11.95677 11.94696 11.93723
## [489] 11.92758 11.91800 11.90849 11.89905 11.88968 11.88038 11.87115 11.86198
## [497] 11.85288 11.84384 11.83486 11.82594 11.81707 11.80827 11.79952 11.79083
## [505] 11.78218 11.77359 11.76505 11.75656 11.74812 11.73972 11.73137
#assign fits to a vector
both_trendc <- fit_bothc
#extract y min and max for each
limits_bothc <- ggplot_build(extract_bothc)$data
## `geom_smooth()` using formula 'y ~ x'
limits_bothc <- as.data.frame(limits_bothc)
both_yminc <- limits_bothc$ymin
both_ymaxc <- limits_bothc$ymax
#reassign dataframes (just to be safe)
work_bothc <- wrfc_both
#fill in missing dates to smooth fits
work_bothc <- work_bothc %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_bothc <- work_bothc$date
#create a new smooth dataframe to layer
smooth_frame_bothc <- data.frame(date_vec_bothc, both_trendc, both_yminc, both_ymaxc)
#WRF C
#plot smooth frames
p_wrf_c <- plotly::plot_ly() %>%
plotly::add_lines(x = ~date_vec_bothc, y = ~both_trendc,
data = smooth_frame_bothc,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_bothc,
'</br> Median Log Copies: ', round(both_trendc, digits = 2)),
line = list(color = '#E7298A', size = 8, opacity = 0.65),
showlegend = FALSE) %>%
layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_bothc, ymin = ~both_yminc, ymax = ~both_ymaxc,
showlegend = FALSE,
opacity = 0.25,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_bothc, #leaving in case we want to change
'</br> Max Log Copies: ', round(both_ymaxc, digits = 2),
'</br> Min Log Copies: ', round(both_yminc, digits = 2)),
name = "",
fillcolor = '#E7298A',
line = list(color = '#E7298A')) %>%
layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies",
showline = TRUE,
automargin = TRUE)) %>%
layout(xaxis = list(title = "Date")) %>%
layout(title = "WRF C") %>%
plotly::add_markers(x = ~date, y = ~log_total_copies_both,
data = wrfc_both,
hoverinfo = "text",
showlegend = FALSE,
text = ~paste('</br> Date: ', date,
'</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
marker = list(color = '#E7298A', size = 6, opacity = 0.65))
p_wrf_c
save(p_wrf_c, file = "./plotly_objs/p_wrf_c.rda")
save(wrfa_both, file = "./plotly_objs/wrfa_both.rda")
save(wrfb_both, file = "./plotly_objs/wrfb_both.rda")
save(wrfc_both, file = "./plotly_objs/wrfc_both.rda")
save(date_vec_botha, file = "./plotly_objs/date_vec_botha.rda")
save(date_vec_bothb, file = "./plotly_objs/date_vec_bothb.rda")
save(date_vec_bothc, file = "./plotly_objs/date_vec_bothc.rda")
save(both_ymina, file = "./plotly_objs/both_ymina.rda")
save(both_ymaxa, file = "./plotly_objs/both_ymaxa.rda")
save(both_yminb, file = "./plotly_objs/both_yminb.rda")
save(both_ymaxb, file = "./plotly_objs/both_ymaxb.rda")
save(both_yminc, file = "./plotly_objs/both_yminc.rda")
save(both_ymaxc, file = "./plotly_objs/both_ymaxc.rda")